/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;

import edu.pku.sei.modeler.editpolicies.NestedNodeLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.figures.ArtifactFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.Artifacts.Artifact;
import edu.pku.sei.modeler.uml.ResourceFactory;
import edu.pku.sei.modeler.uml.commands.ChangeNameCommand;

/**
 * @generated
 * 
 */
public class ArtifactEditpart extends AbstractUMLNodeEditPart {
	public ArtifactEditpart(MetaShapeElement model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		Label header = new Label("<<Artifact>>");
		header.setFont(ResourceFactory.getTextFont());
		header.setForegroundColor(ColorConstants.blue);
		ArtifactFigure figure = new ArtifactFigure(header);
		return figure;
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return new DirectEditPolicy() {
			protected Command getDirectEditCommand(DirectEditRequest request) {
				return new ChangeNameCommand(getArtifact(), (String) request
						.getCellEditor().getValue());
			}

			protected void showCurrentEditValue(DirectEditRequest request) {
				IFigure fig = getDirectEditFigure();
				if (fig instanceof Label) {
					((Label) fig).setText(getArtifact().getFileName());
					fig.getUpdateManager().performUpdate();
				}
			}
		};
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return ((ArtifactFigure) this.getFigure()).getArtifactLable();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE,
				new NestedNodeLayoutEditPolicy(((MetaNode)this.getModel()).getDiagram().getType()));
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		if (propertyName.equals("name")) {
			this.getArtifact().setFileName(this.getArtifact().getName());
		} else if (propertyName.equals("fileName")) {
			this.getArtifact().setName(this.getArtifact().getFileName());
		}
		super.handlePropertyChanged(event);
		refreshVisuals();
	}

	private Artifact getArtifact() {
		return (Artifact) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		Artifact modelEle = (Artifact) getNode().getModelElement();
		ArtifactFigure figure = (ArtifactFigure) getFigure();

		figure.getArtifactLable().setText("" + modelEle.getFileName());

	}
	
	@Override
	public IFigure getContentPane() {
		return ((ArtifactFigure)getFigure()).getBody();
	}
}
